/**
 * 
 */
package com.explgo.backend.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.explgo.backend.domain.Plan;
import com.explgo.backend.service.GenericException;
import com.explgo.backend.service.PlanService;

/**
 * The plan rest service.
 * 
 * @author Jialiang(Cain) Wang
 */
@Controller
@RequestMapping("/plan")
public class PlanController extends AbstractController {
	@Autowired
	private PlanService planService;

	/**
	 * Gets the plan by id.
	 * */
	@RequestMapping("/{id}")
	@ResponseBody
	public Plan getPlanById(@PathVariable("id") Long id) {
		if (id <= 0) {
			throw new GenericException("plan.error.id.invalid", id);
		}

		Plan plan = planService.get(id);
		return plan;
	}

	/**
	 * Saves the plan.
	 * 
	 * @return the persistent plan object.
	 * */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Plan savePlan(@RequestBody Plan plan) {
		return planService.save(plan);
	}
}
